/*
 *@author: YangJie
 *@date: 2021-04-09 10:49:49
 *@version:
 *@description: 参数设置->公共配置->班组
 */
import React, { Component } from 'react';
import { Table, Form, Card, Tabs, Modal } from 'antd';
import '@/assets/css/settings';
import * as Utils from '../../utils_yyr/action';
import BasicForm from '../../components_yyr/common/basicFormbaobiao';
import { FileDown } from '../../components_yyr/commponent/File';
import Paging from '../../components_yyr/commponent/Paging';
// import 'echarts-gl';
import { euqApplicationName_sg, lotAuth, lotAuth2 } from '../../config';

var date = new Date();
let day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate()
let month = date.getMonth() + 1 ? `0${date.getMonth() + 1}` : date.getMonth() + 1
let year = date.getFullYear()
let todayEnd = `${year}-${month}-${day} 23:59:59`
let todayStart = `${year}-${month}-01 00:00:00`
const { TabPane } = Tabs;
class departmentAnalyse extends Component {

    state = {
        loading: false, //加载中...
        dataSource: [], //数据源
        drawerVisible: false, //新增修改抽屉的显示与隐藏
        chooseData: {}, //用checkbox选择好的数据
        total: 0, //总数据量
        formValues: {
            pageNum: 1,
            pageSize: 10,
            startTime: todayStart,
            endTime: todayEnd,
        },
        weekList: [],
        value: '1',
        //  activeKey:'top',
        activeKey: 'top',
        panes: [],
        newTabIndex: 0,
        equipmentTypeChart: [],
        thisMonth: [], lastMonth: [], last90Day: [], dateString: [], EquTypeList: [], DepartmentList: [],
        loadingRecord: false,
        recordTable: [],
        total1: 0,
        formValues1:{
            pageNum: 1,
            pageSize: 10,
        }
    };
    constructor(props) {
        super(props);
        // console.log(props);
    }
    componentDidMount() {
        this.getDate()
        this.fetch({
            pageNum: 1,
            pageSize: 10,
            startTime: todayStart,
            endTime: todayEnd,
        });
        this.findEquTypePageList()
        this.queryTreeDepartment()
    }
    //初始化加载数据
    fetch = (data) => {
        this.setState({ loading: true, formValues: data })
        this.post(`${euqApplicationName_sg}/inspectionEquExecution/findInspectionEquExecutionChart`, { data }).then(res => {
            this.setState({
                loading: false,
                dataSource: res.data,
                total: res.total,
            });
        })
    }

    findEquTypePageList = () => {
        this.get(`${euqApplicationName_sg}/parameter_setting_equipmentType/findEquTypePageList`).then(res => {
            this.setState({
                EquTypeList: res.data
            });
        })
    }
    queryTreeDepartment = () => {
        // this.get(`${lotAuth}/department/queryTreeDepartment`).then(res => {
        this.get(`${lotAuth2}/department/queryTreeDepartment`).then(res => {
            this.setState({
                DepartmentList: res.data
            });
        })
    }
    findInspectionRecordTable = (data) => {
        this.setState({ loadingRecord: true, formValues1: data })
        this.post(`${euqApplicationName_sg}/inspectionEquExecution/findNotScanCodeChart`, { data }).then(res => {
            this.setState({
                loadingRecord: false,
                recordTable: res.data,
                total1: res.total,
            });
        })
    }
    getDate = () => {
        var date = new Date();
        let day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate()
        let month = date.getMonth() + 1 ? `0${date.getMonth() + 1}` : date.getMonth() + 1
        let year = date.getFullYear()
        let todayEnd = `${year}-${month}-${day} 23:59:59`
        let todayStart = `${year}-${month}-01 00:00:00`
        let thisMonth = [todayStart, todayEnd]
        let lastMonth = this.getLastMonth()
        let last90Day = [this.getLast90Day(), todayEnd]
        this.setState({
            thisMonth, lastMonth, last90Day
        })
    }
    getLastMonth = () => {//获取上个月日期第一天
        var nowDate = new Date();
        var year = nowDate.getFullYear();
        var month = nowDate.getMonth() < 10 ? `0${nowDate.getMonth()}` : nowDate.getMonth();
        if (month == 0) {
            year = year - 1;
            month = 12;
        }
        var lastDay = new Date(year, month, 0);
        var day = lastDay.getDate() < 10 ? `0${lastDay.getDate()}` : lastDay.getDate();
        var yyyyMMdd = year + "-" + month + "-" + day + ' 23:59:59';
        return [year + "-" + month + "-01 00:00:00", yyyyMMdd];
    }
    /**
     * 获取上个月月底日期
     */
    getLast90Day = () => {
        var nowDate = new Date();
        var year = nowDate.getFullYear();
        var month = nowDate.getMonth() - 2 < 10 ? `0${nowDate.getMonth() - 2}` : nowDate.getMonth() - 2;
        if (month == 0) {
            month = 12;
            year = year - 1;
        }
        var lastDay = new Date(year, month, 0);
        var day = lastDay.getDate() < 10 ? `0${lastDay.getDate()}` : lastDay.getDate();
        var yyyyMMdd = year + "-" + month + "-" + day + ' 00:00:00';
        return yyyyMMdd;
    }

    onChangeTabs = activeKey => {
        console.log(activeKey)
        this.setState({ activeKey });
    };

    onEdit = (targetKey, action) => {
        this[action](targetKey);
    };

    //表单查询
    filterSubmit = values => {
        console.log(values)
        values.startTime = values.startTime_endTime && values.startTime_endTime.length === 2 ? Utils.getDate(values.startTime_endTime[0]) + ' 00:00:00' : ''
        values.endTime = values.startTime_endTime && values.startTime_endTime.length === 2 ? Utils.getDate(values.startTime_endTime[1]) + ' 23:59:59' : ''
        delete values['startTime_endTime']
        let data = { ...this.state.formValues, ...values }
        console.log(data)
        this.fetch(data);
    };
    //头部关键字查询部分
    formList = () => {
        const { EquTypeList, formValues, DepartmentList } = this.state;
        const formList = [
            { type: "TIMEFRAME", label: "时间", field: "startTime_endTime", allowClear: true, initialValue: [formValues.startTime, formValues.endTime] ,required:true},
            { type: 'TREESELECT', label: '部门', field: 'depIdList', name: 'name', valueField: 'id', allowClear: true, list: DepartmentList, multiple: true },
            { type: 'TREESELECT', label: '类型', field: 'equTypeList', name: 'name', valueField: 'id', allowClear: true, list: EquTypeList, multiple: true },
            { type: 'INPUT', label: '设备名称', field: 'equName', allowClear: true },

        ];
        return formList;
    };
    jump = (record) => {
        this.setState({
            drawerVisible: true
        })
        record.pageNum = 1
        record.pageSize = 10
        this.findInspectionRecordTable(record)
    }
    closeModal = () => {
        this.setState({
            drawerVisible: false
        })
    }
    render() {
        const {  dataSource, loading, total, formValues,loadingRecord,total1, formValues1 ,recordTable ,drawerVisible} = this.state;
        const columns = [
            // { title: '序号', width: 100, dataIndex: 'id' },
            { title: '设备编码', width: 100, dataIndex: 'equCode',align:'center', },
            { title: '设备名称', width: 100, dataIndex: 'equName' ,align:'center',},
            { title: '规格型号', width: 100, dataIndex: 'equSpecsModel' ,align:'center',},
            { title: '应巡检次数', width: 100, dataIndex: 'shouldCount',align:'center', },
            { title: '实际巡检次数', width: 100, dataIndex: 'practicalNum' ,align:'center',},
            { title: '跳过次数', width: 100, dataIndex: 'skipCount' ,align:'center',},
            { title: '未检次数', width: 100, dataIndex: 'undetectedNum',align:'center', },
            { title: '异常次数', width: 100, dataIndex: 'abnormityNumber',align:'center', },
            {
                title: '未扫码次数', width: 100, dataIndex: 'notScanCount', render: (text, record) => {
                    return <a onClick={() => { this.jump(record) }}>{text}</a>
                },align:'center',
            },
            {
                title: '异常率(%)', width: 100, dataIndex: 'abnormityRate', align:'center',
            },
        ]
        const p = this
        const modalProps = {
            title: "未扫码信息",
            visible: drawerVisible,
            width: '780px',
            onCancel() {
                p.closeModal();
            },
            okText: '保存',
            cancelText: '取消',
            onOk() {
                p.modalSubmitPeo();
            },
            footer:null
        };
        const columns1 = [
            // { title: '序号', width: 100, dataIndex: 'id' },
            { title: '未扫码原因', dataIndex: 'skipCodeType',align:'center', },
            { title: '计划编码', dataIndex: 'planCode',align:'center', },
            { title: '计划名称', dataIndex: 'planName' ,align:'center',},
            { title: '计划时间', dataIndex: 'planTime' ,align:'center',},
            { title: '巡检时间', dataIndex: 'startTime',align:'center', },
            { title: '巡检人',dataIndex: 'excutorName' ,align:'center',},
        ]
        return (
            <div className="shadow-radius">
                <Tabs hideAdd
                    onChange={this.onChangeTabs}
                    activeKey={this.state.activeKey}
                    type="editable-card"
                    onEdit={this.onEdit}>
                    <TabPane tab="巡检设备执行统计" key="top" closable={false}>
                        <Card>
                            <BasicForm formList={this.formList()} filterSubmit={this.filterSubmit} labelSpan={8} wrapperSpan={12} onRef={ref => (this.child = ref)} />
                        </Card>
                        <div>
                            <Tabs defaultActiveKey="1" tabBarExtraContent={<>
                                <FileDown fileName="巡检设备执行统计报表" buttonName="导出表" method="post" apiPath={`${euqApplicationName_sg}/inspectionEquExecution/exportPlanExecutionStatisticsExcel`} data={{...this.state.formValues}} />
                            </>}>
                                <TabPane tab="巡检信息" key="1">
                                    <div>
                                        <Table columns={columns} rowKey={(record, index) => record.equId} dataSource={dataSource} pagination={false} loading={loading} scroll={{ x: 'calc(700px + 50%)' }} />
                                        <Paging total={total} formValues={formValues} fetch={pager => this.fetch(pager)} />
                                    </div>
                                </TabPane>
                            </Tabs>
                        </div>
                    </TabPane>
                </Tabs>
                <Modal {...modalProps}>
                    <div style={{marginBottom:10}}>

                     <Table columns={columns1} rowKey={(record, index) => record.planCode} dataSource={recordTable} pagination={false} loading={loadingRecord}  scroll={{ y: 400 }} />
                    </div>
                    <div>
                        <Paging total={total1} formValues={formValues1} fetch={pager => this.findInspectionRecordTable(pager)} />
                    </div>
                </Modal>
            </div>
        );
    }
}
export default Form.create()(departmentAnalyse);
